package com.wcx.application.controller;

import com.wcx.application.domian.AdminBean;
import com.wcx.application.service.AdminService;
import com.wcx.application.service.HelloService;
import com.wcx.application.utils.JwtUtils;
import com.wcx.application.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
@CrossOrigin
public class HelloController {
    @Autowired
    HelloService helloService;
    @Autowired
    AdminService adminService;
    @GetMapping("/getInfo")
    public Result getInfo(String token) {
        String username = JwtUtils.getClaimsByToken(token).getSubject();
        AdminBean adminBean = helloService.selectByUserName(username);
        List<AdminBean> list = adminService.searchList(adminBean);
        System.out.println(username);
        return Result.ok().data("name", username).data("avatar", adminBean.getAvatar()).data("name",list.get(0).getName()).data("id",list.get(0).getId());

    }

    @PostMapping("/login")
    public Result login(@RequestBody AdminBean adminBean) {
        System.out.println(adminBean.getUsername());
        String i = helloService.login(adminBean);
        if(i!=null){
            String token = JwtUtils.generateToken(adminBean.getUsername());
            return Result.ok().data("token", token);
        }else{
            return Result.error();
        }
    }

    @PostMapping("/logout")
    public Result logout() {
        return Result.ok();
    }

















//    @GetMapping("/hello")
//    public String hello(String name) {
//        HelloBean helloBean = new HelloBean();
//        helloBean.setName("你和");
//        helloBean.setPasswd("123456");
//        Gson gson = new Gson();
//        String s = gson.toJson(helloBean);
//        return s+name;
//    }
//    @Operation(summary = "获取用户名")
//    @GetMapping("/get1")
//    public String get1(@RequestParam("name") String nickname){
//        return nickname;
//    };
//    @GetMapping("/user/{id}")
//    public String get11(@PathVariable int id){
//        return ""+id;
//    };
//    @RequestMapping(value = "/post1",method = RequestMethod.POST)
//    public String post(){
//        return "post请求1";
//    };
//    @RequestMapping(value = "/post2",method = RequestMethod.POST)
//    public String post2(@RequestBody HelloBean helloBean){
//        Gson gson = new Gson();
//        System.out.println(helloBean.toString());
//        return gson.toJson(helloBean);
//    };
//    @RequestMapping(value = "/post3",method = RequestMethod.POST)
//    public String post3(String name,String passwd){
//        HelloBean helloBean = new HelloBean();
//        helloBean.setName(name);
//        helloBean.setPasswd(passwd);
//        Gson gson = new Gson();
//        String s = gson.toJson(helloBean);
//        return s;
//    };
//
//    @RequestMapping(value = "/upload",method = RequestMethod.POST)
//    public String up(String name, MultipartFile photo, HttpServletRequest request){
//        System.out.println(name);
//
//        System.out.println(photo.getOriginalFilename());
//        System.out.println(photo.getContentType());
//
//        String path = request.getServletContext().getRealPath("/upload/");
//        System.out.println(path);
//        saveFile(photo, path);
//        return "上传成功";
//    }
//
//    private void saveFile(MultipartFile photo, String path) {
//        File file = new File(path);
//        if(!file.exists()){
//            file.mkdir();
//        }
//        File file1 = new File(path + photo.getOriginalFilename());
//        try {
//            photo.transferTo(file1);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    ;
}
